package com.ruantu.clothing.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.ruantu.clothing.entity.PageBean;
import com.ruantu.clothing.util.MyException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruantu.clothing.entity.JsonResponseMessage;
import com.ruantu.clothing.entity.User;
import com.ruantu.clothing.entity.Vip;

import com.ruantu.clothing.service.VipService;

@Controller
@RequestMapping("/vip")
public class VipController {
    private static final Logger logger = LoggerFactory.getLogger(VipController.class);
    @Autowired
    private VipService vipService;


    /**
     * 查询所有会员
     *
     * @param vip
     * @param session
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public JsonResponseMessage findAll(Vip vip, PageBean pageBean, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        User user = (User) session.getAttribute("info");
        if (!StringUtils.isNotBlank(vip.getJgid())) {
            try {
                vip.setJgid(user.getJgid());
            } catch (NullPointerException e) {
                jsonResponseMessage.setCode(400);
                jsonResponseMessage.setMsg("服务器连接已断开，请重新登录进行尝试");
                logger.error(e.getMessage(), e);
                return jsonResponseMessage;
            }
        }
        jsonResponseMessage.setCode(200);
        jsonResponseMessage.setRows(vipService.findList(vip, pageBean));
        jsonResponseMessage.setTotal(pageBean.getTotal());
        return jsonResponseMessage;
    }


    /**
     * 查询所有会员
     *
     * @param vip
     * @param
     * @return
     */
    @RequestMapping("/id")
    @ResponseBody
    public JsonResponseMessage findVipById(Vip vip) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        jsonResponseMessage.setCode(200);
        jsonResponseMessage.setData(vipService.findVipById(vip));
        return jsonResponseMessage;
    }

    /**
     * 充值会员卡
     *
     * @param vip
     * @param
     * @return
     */
    @RequestMapping("/addMoney")
    @ResponseBody
    public JsonResponseMessage chongZhi(Vip vip, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        User user = (User) session.getAttribute("info");
        try {
            vip.setCreateBy(user.getId());
            vip.setLeixing(0);
        } catch (NullPointerException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接已断开，请刷新重试");
            logger.error(e.getMessage(), e);
            return jsonResponseMessage;
        }
        try {
            jsonResponseMessage.setCode(200);
            vipService.addMoney(vip);
        } catch (MyException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
            logger.error(e.getMessage(), e);
        } catch (Exception e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("内部服务器错误，请检查数据重新尝试");
            logger.error(e.getMessage(), e);
        }
        return jsonResponseMessage;
    }

    @RequestMapping("/save")
    @ResponseBody
    public JsonResponseMessage saveVip(Vip vip, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        User user = (User) session.getAttribute("info");
        try {
            vip.setJgid(user.getJgid());
            vip.setCreateBy(user.getId());
        } catch (NullPointerException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接已断开，请重新登录进行尝试");
            logger.error(e.getMessage(), e);
            return jsonResponseMessage;
        }

        try {
            vipService.saveVip(vip);
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
            logger.error(e.getMessage(), e);
        } catch (Exception e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器内部错误，请检查数据重新尝试");
            logger.error(e.getMessage(), e);
        }
        return jsonResponseMessage;
    }

    /**
     * 小票打印查询
     *
     * @param id
     * @return
     */
    @RequestMapping("/findczjl")
    @ResponseBody
    public JsonResponseMessage findVipWithXiaopiao(String id) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        jsonResponseMessage.setData(vipService.findVipWithXiaoPiao(id));
        return jsonResponseMessage;
    }

    /**
     * 冻结会员
     *
     * @param vip
     * @return
     */
    @RequestMapping("/delvip")
    @ResponseBody
    public JsonResponseMessage delVip(Vip vip) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            vipService.updateVipById(vip);
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
            logger.error(e.getMessage(), e);
        } catch (Exception e) {
            jsonResponseMessage.setMsg("服务器内部错误，请检查数据重新尝试");
            jsonResponseMessage.setCode(400);
            logger.error(e.getMessage(), e);
        }
        return jsonResponseMessage;
    }

    /**
     * 查询充值记录
     *
     * @return
     */
    @RequestMapping("/chongzhilist")
    @ResponseBody
    public JsonResponseMessage findChongzhijilu(Vip vip, PageBean pageBean, String startTime, String endTime, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        User user = (User) session.getAttribute("info");
        if (!StringUtils.isNotBlank(vip.getJgid())) {
            try {
                vip.setJgid(user.getJgid());
            } catch (NullPointerException e) {
                jsonResponseMessage.setCode(400);
                jsonResponseMessage.setMsg("服务器连接已断开，请重新登录进行尝试");
                logger.error(e.getMessage(), e);
                return jsonResponseMessage;
            }
        }
        jsonResponseMessage.setRows(vipService.findChongzhiji(vip, pageBean, startTime, endTime));
        jsonResponseMessage.setTotal(pageBean.getTotal());
        jsonResponseMessage.setCode(200);
        return jsonResponseMessage;
    }


}
